package com.bjpowernode.threadpool;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * ThreadPoolExecutor线程池
 *
 * @author Cat老师，关注我，抖音搜索：java512
 */
public class Test04 {

    public static void main(String[] args) {
        //基于Executor框架实现线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                1,
                1,
                15,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(2),
                Executors.defaultThreadFactory(),
                //Executors.privilegedThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy()
        );

        //同时提交4个任务
        threadPoolExecutor.execute(new MyRunnable(1));
        threadPoolExecutor.execute(new MyRunnable(2));
        threadPoolExecutor.execute(new MyRunnable(3));
        threadPoolExecutor.execute(new MyRunnable(4));

        threadPoolExecutor.shutdown();
    }

    static class MyRunnable implements Runnable {
        private int i;

        public MyRunnable(int i) {
            this.i = i;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + ": " + this.i);
        }
    }
}